/**
 * @author yuliang
 *
 */
package com.xuhai.libms.daoImpl;

import java.util.Collection;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xuhai.libms.dao.UserDao;
import com.xuhai.libms.entity.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	public boolean check(User user) {
		return super.getHibernateTemplate().contains(user);
	}

	public void insert(User user) {
		super.getHibernateTemplate().save(user);
	}

	public void modify(User user) {
		super.getHibernateTemplate().update(user);
	}

	public void remove(User user) {
		super.getHibernateTemplate().delete(user);
	}

	public User show(int id) {
		return (User)super.getHibernateTemplate().get(User.class, id);
	}

	public Collection<User> showall() {
		return (Collection)super.getHibernateTemplate().find("from User");
	}
	
	public Collection<User> showByUser(User user,int start,int end) {
		String sql = "from User u where 1=1 ";
		if(user.getUserName()!=null){
			sql+=" and u.userName='"+user.getUserName()+"'";
		}
		if(user.getLib_card()!=-1)
			sql+=" and u.lib_card='"+user.getLib_card()+"'";
		if(user.getUser_type()!=-1){
			sql+=" and u.user_type='"+user.getUser_type()+"'";
		}
			return super.getHibernateTemplate().find(sql).subList(start, end);
	}
	
	public List<Object> showByPage(int start, int max) {
		return (List<Object>)super.getHibernateTemplate().findByExample(User.class, start,max);
	}

	public User showByCard(int lib_card) {
		String sql = "from User where lib_card=:lib_card";
		List<User> list = (List)super.getHibernateTemplate().findByNamedParam(sql, "lib_card", lib_card);
		if(list.size()!=0){
			return (User)list.get(0);
		}
		else 
			return null;
	}

	public int getSize(User user) {
		String sql = "from User u where 1=1 ";
		if(user.getUserName()!=null){
			sql+=" and u.userName='"+user.getUserName()+"'";
		}
		if(user.getLib_card()!=-1)
			sql+=" and u.lib_card='"+user.getLib_card()+"'";
		if(user.getUser_type()!=-1){
			sql+=" and u.user_type='"+user.getUser_type()+"'";
		}
			return super.getHibernateTemplate().find(sql).size();
	}
}
